<?php
    //header("Content-type: text/html; charset=gbk");
    defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );

    class Admin extends Admin_Controller
    {
        /**
         * 后台菜单列表请求,一级二级三级,先去读session如果没有则去数据库找
         *
         */
        public function index()
        {
            $pid = $this->input->get( 'url_id' );
            $level = $this->input->get( 'level' );
            $pid = $pid ?: 0;
            if ( !empty( $_SESSION[ 'auth_url' ] ) ) {
                $data[ 'navigate' ] = [ ];
                foreach ( $_SESSION[ 'auth_url' ] as $k => $v ) {
                    if ( $v[ 'pid' ] == $pid ) {
                        $data[ 'navigate' ][ ] = $v;
                    }
                }
            } else {
                $this->load->model( "admin/admin_model" );
                $list = $this->admin_model->getNavigateList( $pid );
                $data[ 'navigate' ] = $list[ 'data' ];
                //$data[ 'navigate' ] = $this->admin_model->getNavigateList( $pid );
            }
            if ( !$pid ) {
                //一级菜单
                //$this->load->view( "test.html", $data );
                $this->layout->view( 'index', $data );
            } else {
                if ( $level == "second" ) {
                    //二级菜单
                    //$this->load->view( "second", $data );
                    $this->layout->view( 'second', $data );
                } elseif ( $level == "third" ) {
                    //$this->load->view("third",$data);
                    $this->layout->view( 'third', $data );
                }

            }

        }

        /**
         * 测试
         */
        public function test()
        {
            $this->load->view( 'test.html' );
        }

        /**
         * 得到管理员列表
         */
        public function getAdminerList()
        {
            #接受参数
            $d = $this->input->get( 'd' );
            if ( $d == 'd' ) {
                $this->load->model( "admin/admin_model" );
                $return = $this->admin_model->getAdminList( [ 'start_row' => GLOBAL_START_ROW, 'rows' => GLOBAL_ROWS ] );
                if ( !$return[ 'isok' ] ) P::exitError( $return );
                P::exitOk( $return[ 'data' ] );
            }

            $this->layout->view( "admin/getAdminerList" );
        }

        /**
         * 保存管理员
         */
        public function saveAdminer()
        {
            $get = $this->input->get();
            $this->load->model( "admin/admin_model" );
            $result = $this->admin_model->updateAdminInfo( $get );
            Tool::exitOk();
        }

        /**
         * 删除管理员
         */
        public function deleteAdminer()
        {
            $id = $this->input->post( 'id' );
            $this->load->model( "admin/admin_model" );
            $return = $this->admin_model->delete( [ 'id' => $id ] );
            P::exitAll( $return );
        }

        /**
         * 保存管理员权限
         */
        public function saveAdminGroupAcess()
        {
            $id = $this->input->post( 'id' );
            $group_ids = $this->input->post( 'group' );
            $group_ids = explode( ',', $group_ids );
            $this->load->model( "admin/authrule_model" );
            $getGroupAcessList = $this->authrule_model->saveGroupAcess( $id, $group_ids );
        }

        /**
         * 得到日志列表
         */
        public function getLogList()
        {
            #接受参数
            $d = $this->input->get( 'd' );
            if ( $d == 'd' ) {
                $args = [ ];
                if ( !$_SESSION[ 'in_super_group' ] ) {
                    $args[ 'admin_id' ] = $_SESSION[ 'admin_id' ];
                }
                $args[ 'start_page' ] = GLOBAL_START_ROW;
                $args[ 'rows' ] = GLOBAL_ROWS;
                $this->load->model( "admin/admin_model" );
                $logList = $this->admin_model->getLogList( $args );
                P::exitOk( $logList[ 'data' ][ 'rows' ], $logList[ 'data' ][ 'total' ] );
            } else {
                $this->layout->view( "admin/adminLogList" );
            }

        }

        /**
         * 删除管理员日志
         */
        public function deleteAdminLog()
        {
            $ids = $this->input->post( 'id' );

            $this->load->model( 'admin/admin_model' );
            $return = $this->admin_model->deleteAdminLog( [ 'ids' => $ids ] );
            P::exitAll( $return );
        }

        /**
         * 得到管理员组列表
         */
        public function getAdminGroupList()
        {

            #接受参数
            $d = $this->input->get( 'd' );
            #请求数据
            if ( $d == 'd' ) {
                $this->load->model( "admin/admin_model" );
                $grouplist = $this->admin_model->getAdminGroupList( GLOBAL_START_ROW, GLOBAL_ROWS );
                //p($grouplist);
                if ( !$grouplist[ 'isok' ] ) P::exitError( $grouplist );
                P::exitOk( $grouplist[ 'data' ][ 'list' ], $grouplist[ 'data' ][ 'counts' ] );
            }

            #请求界面
            $this->layout->view( "admin/getAdminGroupList" );
        }
        /**
         * 得到管理员组列表树状
         */
        public function getAdminGroupListByTree()
        {

            #接受参数
            $d = $this->input->get( 'd' );
            $admin_id=$this->input->get('id');
            #请求数据
            if ( $d == 'd' ) {
                $this->load->model( "admin/admin_model" );
                $grouplist = $this->admin_model->getAdminGroupList( GLOBAL_START_ROW, GLOBAL_ROWS );
                //v($grouplist['data']['list']);
                $groups_data=$this->admin_model->getGroupsByAdminId(['admin_id'=>$admin_id]);
                foreach($grouplist['data']['list'] as $k=>&$v)
                {
                    foreach($groups_data['data'] as $p=>$q)
                    {
                        if($v['id']==$q)
                        {
                            $v['is_checked']=true;
                        }
                    }
                }
                if ( !$grouplist[ 'isok' ] ) P::exitError( $grouplist );
                P::exitOk( $grouplist[ 'data' ][ 'list' ], $grouplist[ 'data' ][ 'counts' ] );
            }

            #请求界面
            $this->layout->view( "admin/getAdminGroupList" );
        }

        /**
         * 得到栏目列表
         */
        public function getRuleList()
        {
            #接受参数
            $d = $this->input->get( 'd' );
            if ( $d ) {
                $this->load->model( "admin/admin_model" );
                $list = $this->admin_model->getRuleList();
                $list = $this->tree( $list );
                //表单select树形下拉
                if ( $d == 'select' ) {
                    array_unshift( $list, [ 'id' => 0, 'text' => '顶级菜单', 'pid' => 0 ] );
                    P::exitOk( $list );
                }
                P::exitOk( $list );
            }
            $this->output->cache( 1 );
            $this->layout->view( "admin/ruleList" );
        }

        /**得到树状
         *
         * @param        $table
         * @param string $p_id
         *
         * @return array
         */
        public function tree( $table, $p_id = '0' )
        {
            $tree = [ ];
            foreach ( $table as $row ) {
                //$row['state']="closed";
                if ( $row[ 'pid' ] == $p_id ) {
                    $tmp = $this->tree( $table, $row[ 'id' ] );
                    if ( $tmp ) {
                        $row[ 'children' ] = $tmp;
                    } else {
                        $row[ 'leaf' ] = true;
                    }
                    $tree[ ] = $row;
                }
            }

            return $tree;
        }

        /**
         * 保存管理员组
         */
        public function saveAdminGroup()
        {
            $post = $_POST ? $this->input->post() : $this->input->get();
            //编辑
            $this->load->model( "admin/admin_model" );
            $return = $this->admin_model->saveAdminGroup( $post );
            if ( !$return[ 'isok' ] ) P::exitError( $return );
            P::exitOk();
        }

        /**
         * 保存权限
         */
        public function saveAccess()
        {
            $post = $_POST ? $this->input->post() : $this->input->get();
            //编辑
            $this->load->model( "admin/admin_model" );
            $return = $this->admin_model->saveAccess( $post );
            P::exitAll( $return );
        }

        public function tongXunlu()
        {
            p( "开发中" );
        }
    }
